﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using VOL.Core.Utilities;
using VOL.Entity.DomainModels;
using VOL.Entity.DomainModels.BasicData;
using VOL.Entity.DomainModels.EvaluateNurseCare;
using VOL.SmartMecial.Dtos;
using VOL.SmartMecial.Infrastructure;
using VOL.SmartMecial.IServices.DoctorManage;

namespace VOL.SmartMecial.Services.DoctorManage
{
    public class DoctorManageService : IDoctorManageService
    {
        private readonly IBaseRepository<Patient> _patientRepo;
        private readonly IBaseRepository<OutRecord> _outrecordRepo;
        private readonly IBaseRepository<InRecord> _inrecordRepo;
        private readonly IBaseRepository<Patient> patientRepo;
        private readonly IBaseRepository<OutRecord> outrecordRepo;
        private readonly IBaseRepository<InRecord> inrecordRepo;
        private readonly IBaseRepository<Ward> wardRepository;
        private readonly IBaseRepository<PatientReCordModel> patientReCordRepository;

        public DoctorManageService(IBaseRepository<Patient> patientRepo, IBaseRepository<OutRecord> outrecordRepo, IBaseRepository<InRecord> inrecordRepo, IBaseRepository<Ward> wardRepository, IBaseRepository<PatientReCordModel> patientReCordRepository)
        {
            this.patientRepo = patientRepo;
            this.outrecordRepo = outrecordRepo;
            this.inrecordRepo = inrecordRepo;
            this.wardRepository = wardRepository;
            this.patientReCordRepository = patientReCordRepository;
            this._patientRepo = patientRepo;
            this._outrecordRepo = outrecordRepo;
            this._inrecordRepo = inrecordRepo;
        }

        /// <summary>
        ///获取入量信息
        /// </summary>
        /// <param name="patientCode">患者编号</param>
        /// <returns></returns>
        public List<InRecord> GetinRecord(string patientCode)
        {
            return _inrecordRepo.GetAll().Where(m => m.PatientCode == patientCode).ToList();
        }

        /// <summary>
        /// 获取出量信息
        /// </summary>
        /// <param name="patientCode">患者编号</param>
        /// <returns></returns>
        public List<OutRecord> GetOutRecord(string patientCode)
        {
            return _outrecordRepo.GetAll().Where(m => m.PatientCode == patientCode).ToList();
        }

        
        public async Task<WebResponseContent> GetPateientList()
        {
            var res = new WebResponseContent();
            var list = await (from p in patientRepo.GetAll()
                              join w in wardRepository.GetAll()
                              on p.WardCode equals w.WardNumber
                              select new PatientDetailDto
                              {
                                  Name = p.Name,
                                  PatientCode = p.PatientCode,
                                  WardName = w.WardName,
                                 BedCode=p.BedCode,
                                 Age= p.Age,
                                 Sex=p.Sex
                              }).ToListAsync();
            res.OK("查询成功", list);
            return await Task.FromResult(res);
        }

        public async Task<WebResponseContent> GetPatientPatientReCord(string patientCode)
        {
          var res=new WebResponseContent();
            var list=await patientReCordRepository.GetAll().Where(o=>o.PatientCode == patientCode).ToListAsync();
            res.OK("查询成功", list);
            return await Task.FromResult(res);
        }

        public List<Patient> GetPatients()
        {
            return _patientRepo.GetAll().ToList();
        }
    }
}